package com.teamin.service.impl;

import com.teamin.dto.ActiveUserDTO;
import com.teamin.entity.ActiveUser;
import com.teamin.mapper.ActiveUserMapper;
import com.teamin.service.IActiveUserService;
import com.teamin.utils.DateTime;
import com.teamin.utils.HttpKit;
import com.teamin.vo.ActiveUserStatAddedAndActiveVO;
import com.teamin.vo.ActiveUserStatVO;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * 活跃用户 服务实现类
 *
 * Created by e on 2017/11/28
 *
 */
@Service
public class ActiveUserServiceImpl implements IActiveUserService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ActiveUserMapper activeUserMapper;

    @Override
    public boolean save(ActiveUser activeUser) {

        try{
            ActiveUserDTO activeUserDTO = new ActiveUserDTO();
            activeUserDTO.setActiveUserId(activeUser.getActiveUserId());
            activeUserDTO.setQueryDate(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd"));
            if(!this.isExist(activeUserDTO)){

                //获取用户user_agent 数据
                HttpServletRequest request = HttpKit.getRequest();
                String user_agent = request.getHeader("User-Agent");
                UserAgent userAgent = UserAgent.parseUserAgentString(user_agent);
                Browser browser = userAgent.getBrowser();
                OperatingSystem os = userAgent.getOperatingSystem();


                //获取用户 浏览器，操作系统，ip
                activeUser.setUserAgent(user_agent);
                activeUser.setDevice(os.getDeviceType().getName());
                activeUser.setBrowser(browser.getName());
                activeUser.setOs(os.getName());
                activeUser.setIp(HttpKit.getIp());

                logger.info("isNotExist activeUserDTO:{}", JSONObject.fromObject(activeUserDTO));
                logger.info("insert ActiveUser:{}", JSONObject.fromObject(activeUser));
                return this.activeUserMapper.insert(activeUser) > 0;
            }

            logger.info("isExist activeUserDTO:{}", JSONObject.fromObject(activeUserDTO));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return false;
    }

    @Override
    public boolean isExist(ActiveUserDTO activeUserDTO) {


        return this.activeUserMapper.selectCountByActiveUser(activeUserDTO) > 0;
    }

    @Override
    public List<ActiveUser> listActiveUserByActiveUserDTO(ActiveUserDTO activeUserDTO) {

        return this.activeUserMapper.listActiveUserByActiveUserDTO(activeUserDTO);
    }

    @Override
    public List<ActiveUserStatVO> listActiveUserStat(ActiveUserDTO activeUser) {

        //如果没有指定具体日期，默认为当天
        if(StringUtils.isBlank(activeUser.getQueryDate())){
            String queryDate = new DateTime().toString("yyyy-MM-dd");
            activeUser.setQueryDate(queryDate);
        }

        return this.activeUserMapper.listActiveUserStat(activeUser);
    }

    @Override
    public List<ActiveUserStatAddedAndActiveVO> listActiveUserStatAddedActive() {
        return this.activeUserMapper.listActiveUserStatAddedActive();
    }

}
